<?php
function getAllEquipement($isGold=1,$setPrice=0,$prix=0)
{
    $membres = array('tete','torse','dos','bras_d','bras_g','ceinture','jambes','pieds','acolyte');
    $equip = array();
    $bd = sitedef::GetBddConnection();
    foreach($membres as $membre)
    {
        $query = "SELECT * FROM $membre";
        if(($isGold==0 && $membre!="acolyte")||($setPrice==1))
        {
            $query .= " WHERE ";
            if($isGold==0 && $membre!="acolyte")
            {
                $query .= "gold=0";
                if($setPrice==1)
                    $query .= " and prix<=$prix";
            }
            else
            {
                if($setPrice==1)
                    $query .= " prix<=$prix";
            }
        }
//        echo $query;
        $equip[$membre] = $bd->like_array($bd->query($query));
    }
    return $equip;
}

function getBestEquipement($carac,$isGold=1,$setPrice=0,$prix=0)
{
    $membres = array('tete','torse','dos','bras_d','bras_g','ceinture','jambes','pieds','acolyte');
    $equip = array();
    $bd = sitedef::GetBddConnection();
    foreach($membres as $membre)
    {
        $query = "SELECT id FROM $membre";
        if($setPrice==1)
        {
            $query .= " WHERE ";
            $query .= " prix<=$prix";
        }
//        echo $query."<br>";
        $auto[$membre] = $query;
    }
    var_dump($auto);
//    return $equip;
    $combins = array(
        'tetodobra_norm'=>array('tete','torse','dos','bras_d','bras_g'),
        'tetodobra_gold'=>array('tete','torse','dos','bras_d','bras_g'),
        'cejapiac_norm'=>array('ceinture','jambes','pieds','acolyte'),
        'cejapiac_gold'=>array('ceinture','jambes','pieds','acolyte')
    );
    $selection = array();
    foreach ($combins as $combi=>$membres)
    {
        if((($combi != 'tetodobra_gold' && $combi != 'cejapiac_gold'&& $isGold==0))||($isGold==1))
        {
            $query = "(SELECT ";
            foreach($membres as $membre)
            {
                    $query .= "id_$membre, ";
            }
            foreach($carac as $id=>$crit)
            {
                $query .= $crit;
                if (isset($carac[$id+1]))
                {
                    $query .= ", ";
                }
            }
            $query .= " FROM $combi WHERE ";
            foreach($membres as $id=>$membre)
            {
                if($id!=0)
                    $query .= " AND id_$membre IN (".$auto[$membre].")";
                else
                    $query .= "id_$membre IN (".$auto[$membre].")";
            }
            $query .= " ORDER BY ";
            foreach($carac as $id=>$crit)
            {
                $query .= $crit;
                if (isset($carac[$id+1]))
                {
                    if($crit != "vit" && $crit != "fpf" )
                        $query .= " DESC, ";
                    else
                        $query .= " ASC, ";
                }
                else
                {
                    if($crit != "vit" && $crit != "fpf" )
                        $query .= " DESC";
                    else
                        $query .= " ASC";
                }
            }
            //echo $query."<br><br>";
            $query .= " LIMIT 1)";
            $selection[$combi] = $query;
        }
    }
    if($isGold==1)
    {
        $total['tetodobra'] = $selection['tetodobra_norm']." UNION ".$selection['tetodobra_gold'];
        $total['cejapiac'] = $selection['cejapiac_norm']." UNION ".$selection['cejapiac_gold'];

        $ordre = " ORDER BY ";
        foreach($carac as $id=>$crit)
        {
            $ordre .= $crit;
            if (isset($carac[$id+1]))
            {
                if($crit != "vit" && $crit != "fpf" )
                    $ordre .= " DESC, ";
                else
                    $ordre .= " ASC, ";
            }
            else
            {
                if($crit != "vit" && $crit != "fpf" )
                    $ordre .= " DESC";
                else
                    $ordre .= " ASC";
            }
        }

        $total['tetodobra'] .= " $ordre LIMIT 1";
        $total['cejapiac'] .= " $ordre LIMIT 1";
    }
    else
    {
        $total['tetodobra'] = $selection['tetodobra_norm'];
        $total['cejapiac'] = $selection['cejapiac_norm'];
    }
    echo $total['tetodobra']."<br><br>".$total['cejapiac'];
    $result_haut = $bd->fetch_row($bd->query($total['tetodobra']));
    $result_bas = $bd->fetch_row($bd->query($total['cejapiac']));

    $membres = array(
        'tete'=>$result_haut[0]['id_tete'],
        'torse'=>$result_haut[0]['id_torse'],
        'dos'=>$result_haut[0]['id_dos'],
        'bras_d'=>$result_haut[0]['id_bras_d'],
        'bras_g'=>$result_haut[0]['id_bras_g'],
        'ceinture'=>$result_bas[0]['id_ceinture'],
        'jambes'=>$result_bas[0]['id_jambes'],
        'pieds'=>$result_bas[0]['id_pieds'],
        'acolyte'=>$result_bas[0]['id_acolyte']
    );
    $equip = array();
    foreach($membres as $membre=>$id)
    {
        $query = "SELECT * FROM $membre WHERE id=$id";
//        echo $query;
        $equip[$membre] = $bd->like_array($bd->query($query));
    }
    return $equip;
}

?>
